package com.atguigu.daijia.dispatch.xxl.job;

import com.atguigu.daijia.dispatch.mapper.XxlJobLogMapper;
import com.atguigu.daijia.dispatch.service.NewOrderService;
import com.atguigu.daijia.model.entity.dispatch.XxlJobLog;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * @author DarkClown
 * @date 2024-10-22 13:29
 * @project_name daijia-parent
 * @parem
 **/
@Component
public class JobHandler {
    @Resource
    XxlJobLogMapper xxlJobLogMapper;

    @Resource
    NewOrderService newOrderService;


    @XxlJob("newOrderTaskHandler")
    public void newOrderTaskHandler()
    {

        //记录调度日志
        XxlJobLog xxlJobLog = new XxlJobLog();
        xxlJobLog.setJobId(XxlJobHelper.getJobId());
        long startTime = System.currentTimeMillis();
        try {
            //执行任务:搜索附近代驾司机
            newOrderService.executeTask(XxlJobHelper.getJobId());
            xxlJobLog.setStatus(1);


        }catch (Exception e){
            //失败状态
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(e.getMessage());
            e.printStackTrace();
        }finally {

            //获取执行时间
            long times = System.currentTimeMillis() - startTime;

            xxlJobLog.setTimes(times);

            xxlJobLogMapper.insert(xxlJobLog);

        }
    }
}
